package com.service.Impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import com.utils.BaseService;
import com.utils.DataTables;
import com.utils.RequestStatus;
import com.dao.ScrapMapper;
import com.entity.Scrap;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.ScrapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class ScrapServiceImpl extends BaseService implements ScrapService{
    @Autowired
    private ScrapMapper scrapMapper;
    @Override
    public long getScrapRowCount(){
        return scrapMapper.getScrapRowCount();
    }
    @Override
    public List<Scrap> selectScrap(){
        return scrapMapper.selectScrap();
    }
    @Override
    public Scrap selectScrapByObj(Scrap obj){
        return scrapMapper.selectScrapByObj(obj);
    }
    @Override
    public Scrap selectScrapById(Integer id){
        return scrapMapper.selectScrapById(id);
    }
    @Override
    public int insertScrap(Scrap value){
        return scrapMapper.insertScrap(value);
    }
    @Override
    public int insertNonEmptyScrap(Scrap value){
        return scrapMapper.insertNonEmptyScrap(value);
    }
    @Override
    public int insertScrapByBatch(List<Scrap> value){
        return scrapMapper.insertScrapByBatch(value);
    }
    @Override
    public int deleteScrapById(Integer id){
        return scrapMapper.deleteScrapById(id);
    }
    @Override
    public int updateScrapById(Scrap enti){
        return scrapMapper.updateScrapById(enti);
    }
    @Override
    public int updateNonEmptyScrapById(Scrap enti){
        return scrapMapper.updateNonEmptyScrapById(enti);
    }

    @Override
    public List<Scrap> selectScrapByName(String scrapName) {
        return scrapMapper.selectScrapByName(scrapName);
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {
        PageHelper.startPage((dataTables.getStart() / dataTables.getLength()) + 1, dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("sort asc");

        //判断需要排序的类是否为空
        if(!StringUtils.isEmpty(dataTables.getColumn())){
            String column = propertyToColumn(ScrapMapper.class, dataTables.getColumn());
            if(!StringUtils.isEmpty(column)){
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }

        PageInfo<Scrap> pageInfo = new PageInfo<>(scrapMapper.getPageList(dataTables.getSearch(), dataTables.getSubSQL()));
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<Scrap>());

        return dataTables;
    }

    @Override
    public Map<String, Object> addScrap(Scrap scrap) {
        Map<String, Object> map = new HashMap<>();
        Integer id = scrapMapper.insertScrap(scrap);
        if (id > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }

        return map;
    }

    @Override
    public Map<String, Object> updateScrap(Scrap scrap) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (scrapMapper.updateScrapById(scrap) > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }
        return map;
    }

    @Override
    public Scrap getScrapById(Integer id) {
        return scrapMapper.selectScrapById(id);
    }

    @Override
    public Map<String, Object> delScrap(List<Integer> idlist) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (scrapMapper.deleteByPrimaryKeys(idlist) > 0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }
        return map;
    }

    public ScrapMapper getScrapMapper() {
        return this.scrapMapper;
    }

    public void setScrapMapper(ScrapMapper scrapMapper) {
        this.scrapMapper = scrapMapper;
    }

}